#include "be_camera.h"
#include <cmath>

using namespace std;

BeCamera::BeCamera() : m_sceneNode(0)
{
	//TODO: remove settings from here, it should be handled at a higher level
	settings = Settings::Instance();
}

btVector3 BeCamera::getScreenDirection(const int& x, const int& y)
{
	btTransform transform;
	if(m_sceneNode)
	{
		transform=m_sceneNode->getTransform();
	}
	else
	{
		transform.setIdentity();
	}
	float directionlength = 1000000.f;

	btVector3 forwardRay( 
		-transform.getBasis()[0][2], 
		-transform.getBasis()[1][2], 
		-transform.getBasis()[2][2]); 
	forwardRay *=  directionlength;

	btVector3 upRay( 
		transform.getBasis()[0][1], 
		transform.getBasis()[1][1], 
		transform.getBasis()[2][1]); 

	btVector3 hor = forwardRay.cross(upRay);
	hor.normalize();
	hor *= directionlength;

	upRay = hor.cross(forwardRay);
	upRay.normalize();
	upRay *= directionlength * ((float)*settings->winHeight / *settings->winWidth);

	btVector3 rayTo = (transform.getOrigin() + forwardRay) - (0.5f * hor) + (0.5f * upRay);
	rayTo += x * (hor * (1.0f/((float)*settings->winWidth)));
	rayTo -= y * (upRay * (1.0f/((float)*settings->winHeight)));

	return rayTo;
}




